#include "include.h"

//PC6 TIM3-CH1  PC7 TIM3-CH2 PC8 TIM3-CH3 PC9 TIM3-CH4

void l298n_wheel_config()
{
	GPIO_InitTypeDef GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
	
	//2.使能定时器2和相关IO口时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
	
	//3.初始化IO口为复用功能输出
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;          //电机IN1、IN2、IN3、IN4引脚
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF ;//复用模式
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP ;//上拉
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_25MHz;//速度
	GPIO_Init(GPIOC, &GPIO_InitStruct);
	
	//4.GPIO复用映射到定时器
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);         //将PC6引脚配置为定时器3的PWM功能
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);         //将PC7引脚配置为定时器3的PWM功能
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3);         //将PC8引脚配置为定时器3的PWM功能
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM3);         //将PC9引脚配置为定时器3的PWM功能
	
	//5.初始化定时器：ARR,PSC等  延时1MS
	TIM_TimeBaseInitStruct.TIM_Prescaler = 8400 -1;//预分频系数  1000 000HZ  1秒钟计数1000 000个数 
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up ;//向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 100-1;//设置自动重装载值 --计数值
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分频因子 默认为1
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
	
	//6.初始化输出比较参数
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//PWM模式1
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//输出通道使能
	TIM_OCInitStruct.TIM_Pulse = 0;//CCR1 捕获比较寄存器的值
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;//输出极性  设置有效/默认电平 
	TIM_OC1Init(TIM3, &TIM_OCInitStruct);
	
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//PWM模式1
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//输出通道使能
	TIM_OCInitStruct.TIM_Pulse = 0;//CCR1 捕获比较寄存器的值
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;//输出极性  设置有效/默认电平 
	TIM_OC2Init(TIM3, &TIM_OCInitStruct);
	
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//PWM模式1
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//输出通道使能
	TIM_OCInitStruct.TIM_Pulse = 0;//CCR1 捕获比较寄存器的值
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;//输出极性  设置有效/默认电平 
	TIM_OC3Init(TIM3, &TIM_OCInitStruct);
	
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//PWM模式1
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//输出通道使能
	TIM_OCInitStruct.TIM_Pulse = 0;//CCR1 捕获比较寄存器的值
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;//输出极性  设置有效/默认电平 
	TIM_OC4Init(TIM3, &TIM_OCInitStruct);
	//7.使能预装载寄存器
	TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable); 
	TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable); 
	TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable); 
	TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable); 
	//8.使能自动重装载的预装载寄存器允许位    
	TIM_ARRPreloadConfig(TIM3,ENABLE);
	
	//9.使能定时器
	TIM_Cmd(TIM3,ENABLE);
		
	//停止
	TIM_SetCompare1(TIM3,0); //IN1   左轮正转
	TIM_SetCompare2(TIM3,0); //IN2   左轮反转
	TIM_SetCompare3(TIM3,0); //IN3   右轮正转
	TIM_SetCompare4(TIM3,0); //IN4   右轮正转 
	
	printf("l298n_wheel_config ok\r\n");
}


